package Thread;

import java.util.concurrent.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: homechen
 * Date: 2024-03-12
 * Time: 23:13
 */
public class ThreadDemo1 {
    private static void isCompletedByTaskCount(ThreadPoolExecutor executor) {
        while (executor.getTaskCount() != executor.getCompletedTaskCount()) {
            // 如果不想等，就继续执行
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        // 创建任务
        FutureTask<Integer> task1 = new FutureTask<>(() -> {
            System.out.println("task1 begin!");
            Thread.sleep(1000);
            System.out.println("task1 end!");
            return 1;
        });

        FutureTask<Integer> task2 = new FutureTask<>(() -> {
            System.out.println("task2 begin!");
            Thread.sleep(1500);
            System.out.println("task2 end!");
            return 2;
        });

        FutureTask<Integer> task3 = new FutureTask<>(() -> {
            System.out.println("task3 begin!");
            Thread.sleep(2000);
            System.out.println("task3 end!");
            return 3;
        });

        // 添加任务到线程池
        executor.submit(task1);
        executor.submit(task2);
        executor.submit(task3);

        // 等待所有任务执行完，并获取返回结果
        int ret1 = task1.get();
        int ret2 = task2.get();
        int ret3 = task3.get();

        System.out.println("Task Run Down!");
    }
}
